import arcgis from "./arcgis";
import tdt from "./tdt";
import bing from "./bing";
import osm from "./osm";
import tileImagery from "./tileImagery";
import webTileImagery from "./webTileImagery";
import graticule from "./graticule";
import singletiles from "./singletiles/index.js";

let providers = {
  ...arcgis,
  ...tdt,
  ...bing,
  ...osm,
  ...graticule,
  ...tileImagery,
  ...webTileImagery,
  ...singletiles
}

function all() {
  return providers;
}

function get(type) {
  const provider = providers[type];
  return provider && provider() || null;
}

const $props = ["title"]
/**
 * @param {Object} type
 * @param {Object} options
 * @param {Object} viewer optional
 */
function create(type, options, viewer) {
  const providerDef = get(type);
  const provider = (providerDef && providerDef.create(options, viewer) || null);
  if(provider){
    provider.name=providerDef.name
    $props.forEach(k=>provider[k]=options[k]||providerDef[k])
  }
  return provider
}

/**
 * @param {Object} opts
 * @param {Object} viewer optional
 */
function createImageProvider(opts, viewer) {
  if (!opts) {
    return null;
  }
  if (opts.hasOwnProperty("defaultAlpha")) {
    return opts // ImageProvide 对象
  }
  if (typeof(opts) === "string") {
    opts = {
      type: opts
    }
  }
  if (opts.type) {
    if (opts.type === "default") {
      opts.title = opts.title||"默认影像";
      return null;
    } else {
      const provider = create(opts.type, opts.options, viewer);
      return provider;
    }
  }
  return null;
}

export {
  all,
  get,
  createImageProvider
};
